package appLoader.soundLibrary
{
	import appLoader.I_Loader;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
			
	public class SoundAdmin extends EventDispatcher implements I_Loader
	{
		public static const LOADING:String = "Loading";
		public static const LOAD_COMPLETE:String = "Load Complete";
		public static const LOAD_ITEM_PROGRESS:String = "Load item Progress";
		
		private var _newSoundItem:LoadSoundItem;
		private var _newSoundLibrary:Array;
		private var _libraryTotalSize:uint;
		private var _partialProgress:int;
		private var _progress:int;
		private var _timerProgress:Timer;
		private var _isLoaded:Boolean;
		
		private static var logger:ILogger = Log.getLogger("SoundAdmin");
		
		public function SoundAdmin()
		{
			logger.log(LogEventLevel.INFO, "SoundAdmin");
			_newSoundLibrary = new Array;
			_timerProgress =new Timer(1);
			_timerProgress.addEventListener(TimerEvent.TIMER, itemLoading);
		}
		
		public function initLoad():void{
			for each (var tempSound:LoadSoundItem in _newSoundLibrary) {
				tempSound.addEventListener(LoadSoundItem.SOUND_LOAD_COMPLETE, itemLoaded);
				tempSound.initLoad();
			}
			_timerProgress.start();
		}
		
		protected function itemLoading(event:TimerEvent):void
		{
			_partialProgress = 0;
			progress = 0;
			for each(var tempSound:LoadSoundItem in _newSoundLibrary) {
				_partialProgress+=tempSound.percent
			}
			
			progress=_partialProgress/_newSoundLibrary.length;
			if (progress>=100) {
				_timerProgress.stop();
				_timerProgress.reset();
			}
			dispatchEvent(new Event(LOAD_ITEM_PROGRESS));
		}
		
		private function itemLoaded(event:Event):void {
			var soundLoadedCounter:int = 0;
			for each (var tempSoundItem:LoadSoundItem in _newSoundLibrary) {
				if (tempSoundItem.soundLoaded) {
					soundLoadedCounter++;
				}
				if (soundLoadedCounter==_newSoundLibrary.length) {
					_isLoaded=true;
					dispatchEvent(new Event(SoundAdmin.LOAD_COMPLETE));
				}
			}
		}

		public function setItem(valueId:String, valueUrl:String):void
		{
			if (valueUrl!=""){
				_newSoundItem = new LoadSoundItem(valueId, valueUrl);
			}
			_newSoundLibrary.push(_newSoundItem);
		}
		
		public function getItem(value:String):LoadSoundItem
		{
			var result:LoadSoundItem;
			if (value!=""){
				for each (var tempSound:LoadSoundItem in _newSoundLibrary) {
					if (tempSound.id == value) {
						result=tempSound;
					}
				}
			}
			return result;
		}

		public function get newSoundLibrary():Array
		{
			return _newSoundLibrary;
		}

		public function set newSoundLibrary(value:Array):void
		{
			_newSoundLibrary = value;
		}
		
		public function muteOnAll():void{
			for each (var tempSound:LoadSoundItem in _newSoundLibrary) {
				tempSound.muteOn();	
			}
		}
		
		public function muteOffAll():void{
			for each (var tempSound:LoadSoundItem in _newSoundLibrary) {
				tempSound.muteOff();	
			}
		}

		public function get progress():int
		{
			return _progress;
		}

		public function set progress(value:int):void
		{
			_progress = value;
		}
		
		public function getPercentLoaded():int {
			return _progress;
		}
		
		public function isLoaded():Boolean{
			return _isLoaded;
		}
	}
}